package ecnu.liwaTest.dd.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import static ecnu.liwaTest.dd.DriverSingleton.getChromeDriver;
import static ecnu.liwaTest.dd.DriverSingleton.waitForWindow;
import static org.hamcrest.CoreMatchers.*;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;

public class BasePage {
    WebDriver driver = getChromeDriver();

    public BasePage getMessage(String mes) {
        waitForWindow(1500);
        //String message = driver.findElement(By.xpath("//div/p[@class = 'el-message__content']")).getText();
        String message = driver.findElement(By.className("el-message__content")).getText();
        assertThat(message, is(mes));
        return this;
    }

    public BasePage getError(String errorText) {
        waitForWindow(1500);
        String text = driver.findElement(By.cssSelector(".el-form-item__error")).getText();
        assertThat(text, is(errorText));
        return this;
    }

    public BasePage searchName(String name) {
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".el-input:nth-child(2) > .el-input__inner")).click();
        driver.findElement(By.cssSelector(".el-input:nth-child(2) > .el-input__inner")).clear();
        if(!name.equals(null)) {
            driver.findElement(By.cssSelector(".el-input:nth-child(2) > .el-input__inner")).sendKeys(name);
            driver.findElement(By.cssSelector(".el-input:nth-child(2) > .el-input__inner")).sendKeys(Keys.ENTER);
        }
        return this;
    }

    public BasePage searchDate() {
        driver.findElement(By.cssSelector(".el-range-input:nth-child(2)")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".is-left .el-date-table__row:nth-child(3) > .available:nth-child(4) span")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".is-left .el-date-table__row:nth-child(3) > .available:nth-child(6) span")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".is-plain > span")).click();
        waitForWindow(1000);
        return this;
    }

    public BasePage searchNameAndDate(String name) {
        driver.findElement(By.cssSelector(".nameInput > .el-input__inner")).click();
        driver.findElement(By.cssSelector(".nameInput > .el-input__inner")).sendKeys(name);

        driver.findElement(By.cssSelector(".el-range-input:nth-child(2)")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".is-left .el-date-table__row:nth-child(3) > .available:nth-child(4) span")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".is-left .el-date-table__row:nth-child(3) > .available:nth-child(6) span")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".is-plain > span")).click();
        waitForWindow(1000);
        return this;
    }

    public BasePage verifyNameSearch() {
        waitForWindow(1000);
        //WebElement table = driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div[2]/div[1]/div[3]/table/tbody"));
        WebElement table = driver.findElement(By.cssSelector("tbody"));
        //System.out.println(table);
        List<WebElement> elements = table.findElements(By.cssSelector(".el-table__row"));

        if(elements.isEmpty()) {
            String message = driver.findElement(By.cssSelector(".el-table__empty-text")).getText();
            assertThat(message, is("暂无数据"));
        }
        else {
            List<String> stringList = new ArrayList<>();
            for (WebElement element : elements) {
                //System.out.println(element.getText());
                stringList.add(element.getText());
            }
            String name = driver.findElement(By.cssSelector(".el-input:nth-child(2) > .el-input__inner")).getText();
            assertThat(stringList, everyItem(containsString(name)));
        }
        return this;
    }

    public BasePage verifyDateSearch(String startDate, String endDate) throws ParseException {
        waitForWindow(1000);
        //WebElement table = driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div[2]/div[1]/div[3]/table/tbody"));
        WebElement table = driver.findElement(By.cssSelector("tbody"));
        List<WebElement> elements = table.findElements(By.cssSelector(".el-table_2_column_10 > .cell > div"));
        waitForWindow(1000);

        if(elements.isEmpty()) {
            String message = driver.findElement(By.cssSelector(".el-table__empty-text")).getText();
            assertThat(message, is("暂无数据"));
        }
        else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat resultSdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            List<Long> stringList = new ArrayList<>();
            for (WebElement element : elements) {
                stringList.add(resultSdf.parse(element.getText()).getTime());
            }
            Long start = sdf.parse(startDate).getTime();
            Long end = sdf.parse(endDate).getTime();
            assertThat(stringList, everyItem(both(lessThan(end)).and(greaterThan(start))));
        }
        return this;
    }

    public BasePage downloadRecord() {
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".el-table__row:nth-child(1) .el-button:nth-child(2) > span")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".el-button--small:nth-child(2) > span")).click();
        driver.findElement(By.cssSelector(".el-table__row:nth-child(1) .el-button:nth-child(1) > span")).click();
        waitForWindow(1000);
//        System.out.println(driver.findElement(By.cssSelector(".content:nth-child(1)")).getText());
//        waitForWindow(2000);
        return this;
    }

    public BasePage batchDownload() {
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".el-table__row:nth-child(1) .el-checkbox__input")).click();
        driver.findElement(By.cssSelector(".el-table__row:nth-child(2) .el-checkbox__input")).click();
        driver.findElement(By.cssSelector(".el-table__row:nth-child(3) .el-checkbox__input")).click();
        driver.findElement(By.cssSelector(".customButton > span")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".el-button--small:nth-child(2) > span")).click();
        waitForWindow(1000);
        return this;
    }

    public BasePage batchDownloadFail() {
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".customButton > span")).click();
        return this;
    }

    public void clickToday() {
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".is-left .el-date-table__row:nth-child(4) > .available:nth-child(4) span")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".is-left .el-date-table__row:nth-child(4) > .available:nth-child(5) span")).click();
    }
}
